package br.unicamp.ic.lsd.wscaa.factories;

import br.unicamp.ic.lsd.wscaa.exception.WSDynamicException;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.NotFoundException;

public abstract class WSExceptionFactory {
	private static ClassPool pool = ClassPool.getDefault();
	private static final CtClass[] NO_ARGS = {};

	private WSExceptionFactory() {

	}

	public static WSDynamicException createException(String ex) {
		try {
			
			pool.importPackage("br.unicamp.ic.lsd.wscaa.exception");
			
			CtClass evalClass = pool.makeClass(ex,
					pool.get("br.unicamp.ic.lsd.wscaa.exception.WSDynamicException")); //mudar para a minha classe WSDynamicException
			
			CtConstructor cons = new CtConstructor(NO_ARGS, evalClass);
			
			cons.setBody("super();");
			
			evalClass.addConstructor(cons);
			
			WSDynamicException wsException = (WSDynamicException) pool.get(ex).toClass()
					.newInstance();

			return wsException;
			
		} catch (RuntimeException e1) {
			e1.printStackTrace();
		} catch (NotFoundException e1) {
			e1.printStackTrace();
		} catch (CannotCompileException e1) {
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		}
		
		return null;
	}

	
	


}
